package lyttest.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lyttest.entity.dto.UserDTO;
import lyttest.entity.po.UserPO;
import lyttest.service.UserService;
import lyttest.uils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/v2")
public class TemplateUserController {
    @Autowired
    private UserService userService;

    @PostMapping("/createOrUpdate")
    public Response<UserPO> createOrUpdate(@RequestBody @Valid UserDTO userDTO) {
        try {
            log.info("创建更新对象Request:{} ", JSON.toJSONString(userDTO));
            return Response.createSuccess(userService.createOrUpdate(userDTO));
        } catch (Exception e) {
            log.error("创建更新对象，入参数: ", JSON.toJSONString(userDTO), e);
            return Response.createError("创建更新对象失败");
        }
    }

    @PostMapping("/queryDetail")
    public Response<UserPO> queryDetail(@RequestBody UserDTO userDTO) {
        try {
            log.info("查询详情Request:{} ", JSON.toJSONString(userDTO));
            return Response.createSuccess(userService.queryDetail(userDTO));
        } catch (Exception e) {
            log.error("查询详情，入参数: ", JSON.toJSONString(userDTO), e);
            return Response.createError("查询详情失败");
        }
    }

    @PostMapping("/delete")
    public Response<UserPO> delete(@RequestBody UserDTO userDTO) {
        try {
            log.info("删除对象Request:{} ", JSON.toJSONString(userDTO));
            return Response.createSuccess(userService.delete(userDTO));
        } catch (Exception e) {
            log.error("删除对象，入参数: ", JSON.toJSONString(userDTO), e);
            return Response.createError("删除对象失败");
        }
    }

    @PostMapping("/page")
    public Response<UserPO> page(@RequestBody UserDTO userDTO) {
        try {
            log.info("分页查询Request:{} ", JSON.toJSONString(userDTO));
            return Response.createSuccess(userService.page(userDTO));
        } catch (Exception e) {
            log.error("分页查询，入参数: ", JSON.toJSONString(userDTO), e);
            return Response.createError("分页查询失败");
        }
    }

}